package com.learn.core.controller.manager;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.learn.common.annotation.Log;
import com.learn.common.annotation.UserLoginToken;
import com.learn.common.enums.BusinessType;
import com.learn.common.enums.OperatorType;
import com.learn.common.enums.ResultEnum;
import com.learn.common.utils.JwtUtil;
import com.learn.common.utils.PageResult;
import com.learn.common.utils.ResultUtil;
import com.learn.common.utils.StringUtils;
import com.learn.common.utils.result.CommonResult;
import com.learn.core.domain.LLevel;
import com.learn.core.domain.LLevelSon;
import com.learn.core.dto.LLevelDTO;
import com.learn.core.service.ILLevelPaperService;
import com.learn.core.service.ILLevelService;
import com.learn.core.service.ILLevelSonService;
import com.learn.framework.web.controller.WebController;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.CollectionUtils;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import java.util.Date;

/**
 * Description
 *
 * @author zhaohongyu
 * @ClassName LLevelController
 * @date 2021.07.09 09:11
 * @describe 闯关
 */
@Api("闯关")
@RestController
@RequestMapping("/manager/level")
public class LLevelController extends WebController<LLevel> {

    @Autowired
    private ILLevelService ilLevelService;
    @Autowired
    private ILLevelSonService ilLevelSonService;
    @Autowired
    private ILLevelPaperService ilLevelPaperService;

    //列表
    @ApiOperation(value = "获取列表")
    @UserLoginToken()
    @PostMapping("/list")
    public PageResult<LLevel> list(@RequestBody LLevelDTO lLevelDTO){
        try {
            if (lLevelDTO==null||lLevelDTO.getPage()==null||lLevelDTO.getRows()==null){
                return new PageResult<>(null, null, ResultEnum.ERROR);
            }
            PageHelper.startPage(lLevelDTO.getPage(),lLevelDTO.getRows());
            QueryWrapper<LLevel> queryWrapper = new QueryWrapper<>();
            if(StringUtils.isNotEmpty(lLevelDTO.getName()))
                queryWrapper.like("level_name",lLevelDTO.getName());
            if(StringUtils.isNotEmpty(lLevelDTO.getStartTime()))
                queryWrapper.gt("create_time",lLevelDTO.getStartTime());
            if(StringUtils.isNotEmpty(lLevelDTO.getEndTime()))
                queryWrapper.le("create_time",lLevelDTO.getEndTime());
            Page<LLevel> levels = (Page<LLevel>)ilLevelService.list(queryWrapper);
            if (CollectionUtils.isEmpty(levels)) {
                return new PageResult<>(null, null, ResultEnum.NOT_EEIST);
            }
            // 返回
            return new PageResult(levels.getTotal(), levels, ResultEnum.SUCCESS);
        }catch (Exception e){
            e.printStackTrace();
            return new PageResult(null,null,ResultEnum.ERROR);
        }
    }

    //更新
    @ApiOperation(value = "更新")
    @PostMapping("/edit")
    @UserLoginToken
    @Log(title = "更新闯关", businessType = BusinessType.UPDATE, operatorType = OperatorType.SYSTEM)
    public CommonResult<Object> edit(@RequestBody LLevel lLevel, HttpServletRequest request){
        // token
        String token = request.getHeader("token");
        String userId = JwtUtil.getUserId(token);
        if(lLevel!=null){
            lLevel.setUpdateTime(new Date());
            lLevel.setUpdateUser(userId);
            if(ilLevelService.updateById(lLevel)){
                return (CommonResult) ResultUtil.success(ResultEnum.SUCCESS);
            }
        }
        return (CommonResult)ResultUtil.error(ResultEnum.ERROR);
    }

    //新增
    @ApiOperation(value = "添加")
    @PostMapping("/add")
    @UserLoginToken
    @Log(title = "添加闯关", businessType = BusinessType.INSERT, operatorType = OperatorType.SYSTEM)
    public CommonResult<Object> add(@RequestBody LLevel lLevel, HttpServletRequest request){
        // token
        String token = request.getHeader("token");
        String userId = JwtUtil.getUserId(token);
        try {
            if(lLevel!=null) {
                if (StringUtils.isEmpty(lLevel.getLevelName())) {
                    return (CommonResult) ResultUtil.error(ResultEnum.NOT_LEVEL_NAME);
                }
                lLevel.setCreateTime(new Date());
                lLevel.setCreateUser(userId);
                if (ilLevelService.save(lLevel)) {
                    return (CommonResult) ResultUtil.success(ResultEnum.SUCCESS);
                }
            }
        }catch (Exception e){
            return (CommonResult)ResultUtil.error(ResultEnum.ERROR);
        }
        return  (CommonResult)ResultUtil.error(ResultEnum.ERROR);
    }


    //删除
    @ApiOperation(value = "删除")
    @PostMapping("/del")
    @UserLoginToken
    @Log(title = "删除闯关", businessType = BusinessType.DELETE, operatorType = OperatorType.SYSTEM)
    public CommonResult<Object> del(@RequestBody LLevel lLevel){
        try {
            if(lLevel!=null){
                if(lLevel.getLevelId()!=null){
                    QueryWrapper<LLevel> queryWrapper = new QueryWrapper<LLevel>();
                    queryWrapper.in(true, "level_id", lLevel.getLevelId());
                    if(ilLevelService.remove(queryWrapper)){
                        QueryWrapper<LLevelSon> sonQueryWrapper = new QueryWrapper<LLevelSon>();
                        sonQueryWrapper.in(true, "level_id", lLevel.getLevelId());
                        ilLevelSonService.remove(sonQueryWrapper);
                        return (CommonResult)ResultUtil.success(ResultEnum.SUCCESS);
                    }
                }
            }
        }catch (Exception e)
        {
            return (CommonResult)ResultUtil.error(ResultEnum.ERROR);
        }
        return (CommonResult)ResultUtil.error(ResultEnum.ERROR);
    }

    //上架/下架闯关
    @ApiOperation(value = "上架/下架闯关")
    @GetMapping("/editState")
    @UserLoginToken
    @Log(title = "上架/下架闯关", businessType = BusinessType.UPDATE, operatorType = OperatorType.SYSTEM)
    public CommonResult<Object> state(@RequestParam("id")Integer id,@RequestParam("state")String state, HttpServletRequest request){
        // token
        String token = request.getHeader("token");
        String userId = JwtUtil.getUserId(token);
        if(id!=null){
            LLevel lLevel = new LLevel();
            lLevel.setLevelId(id);
            lLevel.setState(state);
            lLevel.setUpdateTime(new Date());
            lLevel.setUpdateUser(userId);
            if(ilLevelService.updateById(lLevel)){
                if("1".equals(state)){
                    ilLevelPaperService.levelPaper(id,userId);
                }
                return (CommonResult) ResultUtil.success(ResultEnum.SUCCESS);
            }
        }
        return (CommonResult)ResultUtil.error(ResultEnum.ERROR);
    }

}
